package org.csu.mypetstore.service;

import com.sun.javafx.collections.MappingChange;
import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.persistence.CategoryDAO;
import org.csu.mypetstore.persistence.ItemDAO;
import org.csu.mypetstore.persistence.ProductDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CatalogService {
    @Autowired
    private CategoryDAO categoryDAO;
    @Autowired
    private ProductDAO productDAO;
    @Autowired
    private ItemDAO itemDAO;

    public Category getCategory(String categoryId){
        return categoryDAO.getCategory(categoryId);

    }
    public List<Category> getCategoryList(){
        return  categoryDAO.getCategoryList();
    }

    public List<Product> getProductList(){
        return productDAO.getProductList();
    }

    public boolean isCategoryIdExist(String categoryId){
        if(getCategory(categoryId)==null){
            return false;
        }
        else return true;
    }
    public boolean isProductIdExist(String productId){
        if(getProduct(productId)==null){
            return false;
        }
        else return true;
    }
    public boolean isItemIdExist(String itemId){
        if(getItem(itemId)==null){
            return false;
        }
        else return true;
    }
    //oldCategoryId是指的修改前它的categoryId，不然不知道改数据库的哪一条
    public void updateCategory(String oldCategoryId,Category category){
        categoryDAO.updateCategory(oldCategoryId,category);
    }

    public void insertCategory(Category category){
        categoryDAO.insertCategory(category);
    }

    public void deleteCategory(String categoryId){
        categoryDAO.deleteCategory(categoryId);
    }

    public Product getProduct(String productId){
        return productDAO.getProduct(productId);
    }

    public List<Product> getProductListByCategory(String categoryId){
        return productDAO.getProductListByCategory(categoryId);
    }

    public List<Product> searchProductList(String keywords){
        return productDAO.searchProductList(keywords);
    }

    public void insertProduct(Product product){
        productDAO.insertProduct(product);
    }

    public void deleteProduct(String productId){
        productDAO.deleteProduct(productId);
    }

    public void updateProduct(String oldProductId,Product product){
        productDAO.updateProduct(oldProductId,product);
    }

    //Item相关
    public int getInventoryQuantity(String itemId){
        return itemDAO.getInventoryQuantity(itemId);
    }

    public void update2InventoryQuantity(String itemId,int quantity){

        itemDAO.update2InventoryQuantity(itemId,quantity);
    }

    public List<Item> getItemListByProduct(String productId){
        return itemDAO.getItemListByProduct(productId);
    }

    public Item getItem(String itemId){
        return itemDAO.getItem(itemId);
    }

    public void deleteItemByItemId(String itemId){
        itemDAO.deleteItemByItemId(itemId);
    }

    public void deleteItemByProductId(String productId){
        itemDAO.deleteItemByProductId(productId);
    }

    public void updateItem(String oldItemId,Item item){
        itemDAO.updateItem(oldItemId,item);
    }

    @Transactional
    public void insertItem(Item item){
        itemDAO.insertItem(item);
        itemDAO.insertInventoryQuantity(item);
    }




    public boolean isItemInStock(String itemId){
        return itemDAO.getInventoryQuantity(itemId) > 0;
    }
}
